
    var myArray = new Array();//删除id的数组
    var nowArr = new Array();//临时存id数组

    /*ajax查询条件*/
    var chnName = ''; //用户名
    var nowPage = 1;//当前页
    var count = 0;//总页


    $(function () {
        query();

        $("#come").click(function () {
            var div1 = '<div class="yk" onclick="cheakitem(this)" style="background: red; margin-top: 5px;">';
            var div2 = '</div>';
            $.each(myArray, function (i, t) {
                $("#shu").append(div1 + t + div2);
            });
        });

        $("#checkall").click(function () {//删除全选
            var ischecked = this.checked;
            $("input:checkbox[name='guids']").each(function (i) {
                this.checked = ischecked;
                if (this.checked) {//全选选中
                    if ($(".che").get(i).checked) {
                        var id = $(".che").get(i).value;
                        if (nowArr.length > 0) {//myArray有值的时候
                            for (var i = 0; i < nowArr.length; i++) {
                                if (nowArr[i] != id) {
                                    nowArr.push(id);
                                    break;
                                }
                            }
                        } else {//第一次不判断
                            nowArr.push(id);
                        }
                    }
                } else {
                    console.log('sb');
                    clearCheck();
                }
            });
            if ($('input[name="guids"]:not(:checked)').length == 5) {
                myArray = [];
                alert(myArray.length);
            }
            if (nowArr.length > 0) {
                myArray = unique(nowArr);
            }
            for (var i = 0; i < myArray.length; i++) {
                console.log('my已选中 ：' + myArray[i]);
            }
            console.log('my长度：' + myArray.length);
            for (var i = 0; i < nowArr.length; i++) {
                console.log('now已选中 ：' + nowArr[i]);
            }
            console.log('now长度：' + nowArr.length);
        });

        var msg = $("#msg").html();
        if (msg != null) {
            alert(msg);
        }
        $('#shang').click(function () {
            if (nowPage > 1) {
                nowPage--;
                query();
                $('#checkall').removeAttr('checked');

            }
        });
        $('#xia').click(function () {
            if (nowPage < count) {
                nowPage++;
                query();
                $('#checkall').removeAttr('checked');
            }
        });
        /*
         * 进入添加页面验证
         * */
        $('#new').click(function () {
            $.ajax({
                type: 'post',
                url: '/webtest/userRole/addAjax2',
                dataType: 'json',
                success: function (data) {
                    if (data == 1) {//所有用户都添加了，需要删除几个才能进入
                        alert('已没有用户可以添加！');
                    }
                    if (data == 0) {
                        window.location.href = "/webtest/userRole/addPage";
                    }
                },
                error: function (error) {
                    alert('数据库没有这个值');
                }
            });
        });
    });

    function search() {
        chnName = $('#chnName').val();
        query();
    }


    function goPage() {//页数跳转
        var page = $('#myPageIndex').val();
        if (page == '') {//判断是否为空字符
            alert('空字符');
        } else {
            var ival = parseInt(page);//如果变量val是字符类型的数则转换为int类型 如果不是则ival为NaN
            if (!isNaN(ival)) {//输入其他字符
            	if(page > count){//输入页比总页大
            		nowPage = count;//就查最后一页
                    query();
            	}else if(page <= 0){//输入第0页
            		nowPage = 1;//就查第1页
                    query();
            	}else{
            		nowPage = page;
                    query();	
            	}
            } else {
                alert(page + "不是数字，重试");
            }
        }
    }

    function chk() {
        /*  var obj = document.getElementsByName('guids'); //选择所有name="'test'"的对象，返回数组
         //取到对象数组后，我们来循环检测它是不是被选中
         var s = '';
         for (var i = 0; i < obj.length; i++) {
         if (obj[i].checked) s += obj[i].value + ','; //如果选中，将value添加到变量s中
         }
         //那么现在来检测s的值就知道选中的复选框的值了
         alert(s == '' ? '你还没有选择任何内容！' : s); */

        $.ajax({
            type: "GET",
            url: "/webtest/userRole/ajax",
            traditional: true,
            data: {'myArray': myArray, 'chnName': chnName},
            dataType: "json",
            success: function (data) {
                var $tbody = $('#tbody');
                $tbody.empty();
                var str = '';
                for (var i = 0; i < data.length; i++) {
                    str += '<tr>';
                    str += '<td style="layout:fixed;height:40px"><input name="guids" value="'
                            + data[i].userRoleId
                            + '" type="checkbox" class="che" onclick="dan(this)"></td>';
                    str += '<td>' + data[i].userRoleId + '</td>';
                    str += '<td>' + data[i].chnName + '</td>';
                    str += '<td>' + data[i].roleName + '</td>';
                    str += '<td>' + data[i].createTime + '</td>';
                    str += '<td>' + data[i].mobile + '</td>';
                    str += '<td><a href="/webtest/userRole/modifyPage?userRoleId=' + data[i].userRoleId + '">修改</a>';
                    str += '<a href="javaScript:void(0)" onclick="delOne(' + data[i].userRoleId + ')">删除</a>';
                    str += '</td>';
                    str += '</tr>';
                }
                $tbody.append(str);
                $('#myPageIndex').val(1);
            }, error: function ($data) {
                window.location.href = '/webtest/userRole/get';
            }
        });
    }

    function delOne(id) {
        $.ajax({
            type: 'post',
            url: '/webtest/userRole/delOne',
            data: {
                'id': id,
            },
            dataType: 'json',
            success: function (data) {
                query();
            },
            error: function (data) {
                alert('删除失败');
            }
        });
    }

    function dan(obj) {//单个选中
        checkSize();
        var id = $(obj).attr('value');
        if (nowArr.length > 0) {//myArray有值的时候
            for (var i = 0; i < nowArr.length; i++) {
                console.log('i : ' + i);
                if (nowArr[i] != id) {
                    nowArr.push(id);
                    break;
                }
            }
        } else {
            nowArr.push(id);
        }
        if (nowArr.length > 0) {
            nowArr = unique(nowArr);
            myArray = nowArr;
        }

        if (!obj.checked) {
            clearCheck();
        }

        for (var i = 0; i < myArray.length; i++) {
            console.log('my已选中 ：' + myArray[i]);
        }
        console.log('my长度：' + myArray.length);
        for (var i = 0; i < nowArr.length; i++) {
            console.log('now已选中 ：' + nowArr[i]);
        }
        console.log('now长度：' + nowArr.length);
    }

    function clearCheck() {//清除选择
        $(":checkbox").each(function () {
            if (this.name == 'guids') {//不选择全选按钮
                if (this.checked == false) {
                    var id = this.value;
                    for (var i = 0; i < nowArr.length; i++) {
                        if (id == nowArr[i]) {
                            nowArr.splice(i, 1);
                        }
                    }
                    for (var j = 0; j < myArray.length; j++) {
                        if (myArray[j] == id) {
                            myArray.splice(i, 1);
                        }
                    }
                }
            }
        });
    }

    function unique(arr, removeAll) {//把临时存放的数组转为唯一的赋给myarray
        arr.sort(function (a, b) {
            return a - b;
        });//先排序
        var s = arr.join(',') + ',';
        arr = s.replace(/(\d+,)\1+/g, removeAll ? '' : '$1').replace(/,$/, '').split(',');
        for (var i = 0; i < arr.length; i++)arr[i] = parseInt(arr[i], 10);//重新转为数字
        return arr;
    }

    function query() {
        $.ajax({
            type: 'post',
            url: '/webtest/userRole/ajaxQuery',
            traditional: true,
            data: {
                'chnName': chnName,
                'myArrays': myArray,
                'pageIndex': nowPage
            },
            dataType: 'json',
            success: function (data) {
                console.log('ajax成功');
                console.log(data.length);
                var $tbody = $('#tbody');
                $tbody.empty();
                var str = '';
                for (var i = 0; i < data.length; i++) {
                    str += '<tr>';
                    if (myArray.length > 0) {
                        var td = '';
                        for (var j = 0; j < myArray.length; j++) {
                            if (myArray[j] == data[i].userRoleId) {
                                td = '<td style="layout:fixed;height:40px"><input name="guids" value="'
                                        + data[i].userRoleId
                                        + '" type="checkbox" class="che" onclick="dan(this)" checked="checked"></td>';
                            }
                        }
                        if (td != '') {
                            str += td;
                        } else {
                            str += '<td style="layout:fixed;height:40px"><input name="guids" value="'
                                    + data[i].userRoleId
                                    + '" type="checkbox" class="che" onclick="dan(this)"></td>';
                        }
                    } else {
                        str += '<td style="layout:fixed;height:40px"><input name="guids" value="'
                                + data[i].userRoleId
                                + '" type="checkbox" class="che" onclick="dan(this)"></td>';
                    }
                    str += '<td>' + data[i].userRoleId + '</td>';
                    str += '<td>' + data[i].chnName + '</td>';
                    str += '<td>' + data[i].roleName + '</td>';
                    str += '<td>' + data[i].createTime + '</td>';
                    str += '<td>' + data[i].mobile + '</td>';
                    str += '<td><a href="/webtest/userRole/modifyPage?userRoleId=' + data[i].userRoleId + '">修改</a>';
                    str += '<a href="javaScript:void(0)" onclick="delOne(' + data[i].userRoleId + ')">删除</a>';
                    str += '</td>';
                    str += '</tr>';
                }
                $tbody.append(str);
                $('#myPageIndex').val(nowPage);
                checkSize();
                getCount(chnName);
            },
            error: function (data) {
                alert('失败');
            }
        });
    }
    function checkSize() {//判断复选框
        //alert('1');
        var selectCheckBox = $("input[name='guids']:checked");//如果页面复选框全选，激活全选按钮
       
        if (selectCheckBox.size() == 4) {
            $('#checkall').attr('checked', 'true');
            //  alert('2');
        } else {
            $('#checkall').removeAttr('checked');
            //alert('3');
        }
    }
    
    function getCount(chnName) {
        $.ajax({
            url: '/webtest/userRole/ajaxCount',
            data: {'chnName': chnName},
            dataType: 'json',
            type: 'post',
            success: function (data) {
            	count = data;
            	var span = $('#span');
                span.empty();
                var str = '<span id="span">共' + data + '页</span>';
                span.append(str);
            }, error: function (error) {
                alert('获取页数失败');
            }
        });
    }